/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.stepapp.persistence;



import android.app.ListActivity;
import android.content.ContentResolver;

import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import java.util.List;
import org.me.stepapp.R;


public class LocationClient extends  ListActivity implements LocationDBInterface {

    View input_view;

    SimpleCursorAdapter  myAdapter;
    Cursor cursor;
    LocationDBAdapter db=null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);


        setContentView(R.layout.list_location);
        db = new LocationDBAdapter(this.getApplicationContext());
        db.open();
        cursor = db.getAllLocations();

        ContentResolver r = getContentResolver();
        cursor = r.query(LocationProvider.CONTENT_URI, null, null, null, null);

        // the cursor is used to "feed" the ListView adapter
        myAdapter = new SimpleCursorAdapter(this,
            R.layout.row,
            cursor,
            new String[] { Location.KEY_TIMESTAMP, Location.KEY_LATITUDE, Location.KEY_LONGITUDE},
            new int[] {R.id.timestamp, R.id.latitude, R.id.longitude}
           ) ;

        setListAdapter(myAdapter );
        startManagingCursor(cursor);

        LocationChangeReceiver intentReceiver = new LocationChangeReceiver(this);
        IntentFilter intentFilter = new IntentFilter("org.me.stepapp.persistence.LocationChangeReceiver");
        registerReceiver(intentReceiver, intentFilter);
    }

    /* Display Timestamp of location at cursor */
    public void displayTimestamp (Cursor c){
        Toast.makeText(this,
            "id: " + c.getString(Location._ID) + "\n" +
            "TIMESTAMP: " + c.getString(Location.TIMESTAMP_ID) + "\n" +
            "LATITUDE: " + c.getString(Location.LATITUDE_ID) + "\n" +
            "LONGITUDE:  " + c.getString(Location.LONGITUDE_ID),
            Toast.LENGTH_LONG).show();
        }

    /* Get location at a particular id */
    public Location getLocation( int id)  {
       return db.getLocation(id);
    }

    /* Get location at a given cursor */
    public Location getLocation( Cursor c)  {
        return db.getLocationFromCursor(c);
    }

    /* Action when updat is performed */
    public void onUpdateLocations() {
        Toast.makeText( this,"updateLocations", Toast.LENGTH_SHORT);
            if ( cursor!=null )  {
                cursor.requery();
            }
            if ( myAdapter!=null ) {
                myAdapter.notifyDataSetChanged();
            }
    }

    public List<Location> retrieveAllLocations() {
        return this.db.retrieveAllLocations();
    }

    public List<Location> retrieveLocations(String where) {
        return db.retrieveLocations(where);
    }

    /* Delete location with location parameter */
    public boolean deleteLocation(Location x) {
        return true;
    }

    /* Delete location using Row id */
    public boolean deleteLocation(int id) {
        return true;
    }

    /* Delete all locations */
    public boolean deleteAllLocations() {
        return true;
    }

    /* Retrieve location using id */
    public Location getLocation(long id) {
       return null;
    }

    /* Insert location using location */
    public long insertLocation(Location x) {
       return -1;
    }

    /* Insert location using timestamp, latitude, and longitude */
    public long insertLocation (String timestamp, String latitude, String longitude) {
       return -1;
    }

    /* Delete location using id */
    public boolean deleteLocation(long id) {
       return false;
    }

    /* Update location using id and location */
    public boolean updateLocation(long rowId, Location x) {
        return false;
    }

}
